<template>
  <div>
    <!-- 校区管理 -->
    <el-row>
      <el-button size="mini" type="primary" @click="dialog_newly = true" icon="el-icon-folder-add">新增</el-button>
      <!-- <el-button type="danger" icon="el-icon-delete" @click="deleteLots">删除</el-button> -->
    </el-row>
    <el-divider></el-divider>
    <!-- 表格 -->
    <el-table size="medium" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" border>
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <!-- <el-table-column prop="id" label="ID" align="center"></el-table-column> -->
      <!-- <el-table-column prop="" label="学校" align="center"></el-table-column> -->
      <el-table-column prop="name" label="校区" align="center"></el-table-column>
      <el-table-column prop="address" label="所在地" align="center"></el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color:#67C23A;">正常</span>
          <span v-if="scope.row.state == 2" style="color:red;">停用</span>
        </template>
      </el-table-column>
      <el-table-column prop="handle" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="updateCampus(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" plain @click="deleteCampus({ id: scope.row.id })">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination class="pagination" background layout="prev, pager, next" :total="total" :page-size="10"
      @current-change="handleCurrentChange"></el-pagination>

    <!-- 添加校区信息弹窗 -->
    <el-dialog title="添加校区信息" :visible.sync="dialog_newly">
      <el-form :model="formNewCampus" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="校区名 :">
          <el-input v-model="formNewCampus.name" style="width:60%" placeholder="请输入校区名" clearable></el-input>
        </el-form-item>
        <el-form-item label="所在地区 :">
          <el-input v-model="formNewCampus.address" style="width:60%" placeholder="请输入所在地区" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态 :">
          <el-select v-model="formNewCampus.state" placeholder="请选择状态" style="width:60%" clearable>
            <el-option label="正常" value="1"></el-option>
            <el-option label="停用" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_newly = false">取 消</el-button>
        <el-button type="primary" @click="addNewCampus" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加校区信息弹窗 -->

    <!-- 修改校区信息弹窗 -->
    <el-dialog title="修改校区信息" :visible.sync="dialog_change">
      <el-form :model="formChangeCampus" :label-position="labelPosition" label-width="100px" style="margin-left:20%;">
        <el-form-item label="校区名 :">
          <el-input v-model="formChangeCampus.name" style="width:60%" placeholder="请输入校区名" clearable></el-input>
        </el-form-item>
        <el-form-item label="所在地区 :">
          <el-input v-model="formChangeCampus.address" style="width:60%" placeholder="请输入所在地区" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态 :">
          <el-select v-model="formChangeCampus.state" placeholder="请选择状态" style="width:60%" clearable>
            <el-option label="正常" value="1"></el-option>
            <el-option label="停用" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" @click="updateCampusConfirm" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改校区信息弹窗 -->
  </div>
</template>

<script>
import { getCampusList, addCampus, updateCampus, deleteCampus } from '_api/DormManage/Campus/index.js'
export default {
  data() {
    return {
      labelPosition: "left",
      dialog_change: false,
      dialog_newly: false,
      tableData: [],
      multipleSelection: [],
      form: {
        current: 0,
        field: "",
        order: "",
        pageSize: 10
      },
      formNewCampus: {
        address: '',
        name: '',
        state: ''
      },
      formChangeCampus: {
        address: '',
        name: '',
        state: ''
      },
      total: 0
    }
  },
  methods: {
    handleSelectionChange(val) {
      var multipleSelection = []
      val.forEach(item => {
        multipleSelection.push(item.id)
      })
      this.multipleSelection = multipleSelection
      var test = []
      this.multipleSelection.forEach(item => {
        var one = { id: item }
        test.push(one)
      })
      let obj = { ...test }
      console.log(obj)
      this.multipleSelection = obj
      // console.log(this.multipleSelection)
    },
    deleteLots() {
      this.deleteCampus(this.multipleSelection)
    },

    //删除校区
    deleteCampus(e) {
      this.$confirm('此操作将删除该校区, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCampus(e).then(res => {
          console.log(res)
          if (res.status == 200) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.getCampusList()
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改校区弹窗
    updateCampus(e) {
      console.log(e)
      this.dialog_change = true
      this.formChangeCampus = JSON.parse(JSON.stringify(e))
    },
    // 修改校区确认
    updateCampusConfirm() {
      updateCampus(this.formChangeCampus).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.dialog_change = false
          this.$message({
            message: '修改成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.getCampusList()
            }
          })
        }
      })

    },
    // 添加校区
    addNewCampus() {
      addCampus(this.formNewCampus).then(res => {
        if (res.status == 200) {
          this.dialog_newly = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.getCampusList()
            }
          })
        }
      })
    },
    // prev(e) {
    //   //上一页
    //   this.form.current = e-1
    //   this.getCampusList()
    // },
    // next(e) {
    //   //下一页
    //   this.form.current = e-1
    //   this.getCampusList()
    // },
    handleCurrentChange(e) {
      //当前页
      this.form.current = e
      this.getCampusList()
    },
    // 查询校区
    getCampusList() {
      getCampusList(
        this.form
      ).then(res => {
        console.log(res);
        if (res.data.success == true) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.$message({
            message: res.data.message,
            duration: 1000
          })
        }
      })
    }
  },
  created() {
    this.getCampusList()
  },
};
</script>
<style lang="scss" scoped>
.pagination {
  float: right;
  margin-top: 10px;
}
</style>
